AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')


function ENT:SpawnFunction( ply, tr)
	local SpawnPos = tr.HitPos + tr.HitNormal * 50
	local ang = ply:GetAimVector():Angle(); ang.p = 0; ang.r = 0; ang.y = (ang.y+180) % 360
	local ent = ents.Create( "prometheus" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent:SetAngles(ang);
	return ent
end

function ENT:Initialize()
	self.MaxRoll = 20; -- dahh, how much of an angle the 303 can roll!
	
	self.PositionSet = false;
	self.Target = Vector(0,0,0)
	
	--Note to self, remove UNDEEDED wire I/O's
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity,{"View", "Disable Weapon"}) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "X", "Y", "Z", "mouse1", "mouse2", "r", "shift"}) end
	self.reloadtime = (4); -- Just WHAT!? we are a sent yes ? not a swep! .... Right because we are mointering R
	
	self.Entity:SetUseType( SIMPLE_USE )
	self.In=false
	self.Pilot=nil
	self.Roll=0;
	self.KeyTime=CurTime();
	self.Entity:SetModel("models/modularpack/prometheus.mdl")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:DrawShadow(true);
		local phys = self.Entity:GetPhysicsObject()
		if (phys:IsValid()) then
			phys:Wake()
			phys:SetMass(10000)
		end
	self.Entity:StartMotionController()
	self.Accel=0
	self.SThink = 0
	self.WeaponTime = 0
	self.newreload = 0
	self.GunTime = 0
	self.UseWeapon = true
	self.WeaponNextFire = 0.6 --1 = One second
	self.GunNextFire = 0.1 -- Test for machine gun like turrets
	self.ProjectileMaxSpeed = 1000
	self.Sound_engineStart = 1
	
	self.Sound_engine = CreateSound( self.Entity, "alienate/F302Engine.wav" )
	
	local max = self.Entity:OBBMaxs()
	self.ThrustOffset 	= Vector( 100, 100, max.z )
	self:SetOffset( self.ThrustOffset )
	self:SetEffect("none");
	
	self.Entity.grav_plate = 1
	if ( SERVER ) then
			self.Entity.EntityMods = self.Entity.EntityMods or {}
			self.Entity.EntityMods.GravPlating = 1
		end
	
end


function ENT:OnRemove()
 	
	if self.In then
	self.Pilot:UnSpectate()
	self.Pilot:DrawViewModel(true)
	self.Pilot:DrawWorldModel(true)
	self.Pilot:Spawn()
	self.Pilot:SetNetworkedBool("isDrivepro",false)
	self.Pilot:SetPos(self.Entity:GetPos()+self.Entity:GetRight() + self.Entity:GetForward()*-1000 + self.Entity:GetUp()*2750)
	self.Sound_engine:Stop()
	self:SetEffect( "none" ) --Turn OFF flame effect
	end
end

function ENT:Think()
self:SetOn(self.In)
local enabled = self:Enabled();
	if not ValidEntity(self.Pilot) then self.Pilot=nil self.In=false end
	if self.In and self.Pilot and self.Pilot:IsValid() then
		if self.Pilot:KeyDown(IN_RELOAD) then 
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "r", 1)
				if self.newreload <= CurTime() then
					self.newreload = CurTime()+0.5
					if self.Entity:GetNetworkedBool("FirstPerson") then
						self.Entity:SetNetworkedBool("FirstPerson",false)
					else
						self.Entity:SetNetworkedBool("FirstPerson",true)
					end
				end
			end
		else
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "r", 0)
			end
		end
	
		if self.Pilot:KeyDown(IN_SPEED) then
			if (!self.Pilot:KeyDown(IN_FORWARD)) then
				self:SetAfterBurner(true);
			end
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "shift", 1)
			end
		else
			self:SetAfterBurner(false);
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "shift", 0)
			end
		end

		if self.Pilot:KeyDown(IN_ATTACK2) then 
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "mouse2", 1)
				if (self.UseWeapon == true) then
					if self.WeaponTime<CurTime() then
						self:FireWeapon()
						self.WeaponTime=CurTime()+self.WeaponNextFire
					end
				end
			end
		else
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "mouse2", 0)
			end
		end
	
		if self.Pilot:KeyDown(IN_ATTACK) then
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "mouse1", 1)
				if self.GunTime<CurTime() then
					self:FireGuns()
					self.GunTime=CurTime()+self.GunNextFire
				end
			end
		else
			if not (WireAddon == nil) then
				Wire_TriggerOutput(self.Entity, "mouse1", 0)
			end
		end
		
		if self.SThink<CurTime() then
			self.Sound_engine:Stop()
			self.Sound_engine:Play()
			self.SThink=CurTime()+3.0
		end
		
		self.Pilot:SetPos(self.Entity:GetPos())

		
		if self.Pilot:KeyDown(IN_USE) then
			self.Pilot:UnSpectate()
			self.Pilot:DrawViewModel(true)
			self.Pilot:DrawWorldModel(true)
			self.Pilot:Spawn()
			self.Pilot:SetNetworkedBool("isDrivepro",false)
			self.Pilot:SetPos(self.Entity:GetPos()+self.Entity:GetRight() + self.Entity:GetForward()*-1000 + self.Entity:GetUp()*2750)

			self.Accel=0
			self.In=false
		
			
			self.Entity:SetLocalVelocity(Vector(0,0,0))
			self.Pilot=nil
			self.Sound_engine:Stop()
			self:SetEffect( "none" ) --Turn OFF flame effect
		end			
			
		
		
		self.Entity:NextThink(CurTime())
	else
		self.Entity:NextThink(CurTime()+1)
	end
	
return true
end

function ENT:Use(ply,caller)
if not self.In then
	ply:Spectate( OBS_MODE_ROAMING ) -- OBS_MODE_ROAMING, OBS_MODE_IN_EYE
	ply:StripWeapons()
	self.Entity:GetPhysicsObject():Wake()
	self.Entity:GetPhysicsObject():EnableMotion(true)
	self.In=true
	ply:DrawViewModel(false)
	ply:DrawWorldModel(false)
	ply:SetNetworkedBool("isDrivepro",true)
	ply:SetNetworkedEntity("Prometheus",self.Entity)
	self.Pilot=ply	
	self.Sound_engine:Play()
	self:SetEffect( "plasma" ); --Flame effect on?
	end
end

function ENT:PhysicsSimulate( phys, deltatime )
	if self.In then
		local num=0
		if self.Pilot:KeyDown(IN_FORWARD) then
            num=500
			self:SetMovingForward(true)
		elseif self.Pilot:KeyDown(IN_BACK) then
            num=-500
			self:SetMovingForward(false)
		elseif self.Pilot:KeyDown(IN_SPEED) then
            num=1500
			self:SetMovingForward(true)
		else
			self:SetMovingForward(false)
		end
			
		phys:Wake()
		self.Accel=math.Approach(self.Accel,num,10)
		local move={}
			move.secondstoarrive	= 1
			move.pos = self.Entity:GetPos()+self.Entity:GetForward()*self.Accel
				if self.Pilot:KeyDown( IN_DUCK ) then
                   move.pos = move.pos+self.Entity:GetUp()*-200
               elseif self.Pilot:KeyDown( IN_JUMP ) then
                   move.pos = move.pos+self.Entity:GetUp()*300
				end
            
                if self.Pilot:KeyDown( IN_MOVERIGHT ) then
					self.KeyTime = CurTime()+2;
					--move.pos = move.pos+self.Entity:GetRight()*400					
					self.Roll = self.Roll + 0.5;
					if(self.Roll > self.MaxRoll) then
						self.Roll = self.MaxRoll;
					end
				elseif self.Pilot:KeyDown( IN_MOVELEFT ) then
					self.KeyTime = CurTime()+2;
					--move.pos = move.pos+self.Entity:GetRight()*-400
					self.Roll = self.Roll - 0.5;   --315 -> 360
					if(self.Roll < -self.MaxRoll) then
						self.Roll = -self.MaxRoll;
					end
				end
			
			if (CurTime() > self.KeyTime) then
				if (self.Roll != 0) then
					if (self.Roll > 0) then
						self.Roll = self.Roll - 0.3;
					elseif(self.Roll < 0) then
						self.Roll = self.Roll + 0.3;
					end
				end
			end
			
			move.maxangular		= 5
			move.maxangulardamp	= 100
			move.maxspeed			= 100000
			move.maxspeeddamp		= 10000
			move.dampfactor		= 0.8
			
			move.teleportdistance	= 5000
			local ang = self.Pilot:GetAimVector():Angle()
			ang.r=ang.r+self.Roll;  --Borrowed from LightDemons Code :D
			move.angle			= ang --+ Vector(90 0, 0)
			move.deltatime		= deltatime
		phys:ComputeShadowControl(move)
	else
		if self.Accel>-200 and self.Accel < 200 then
			phys:EnableMotion(false)
			local oldang = self.Entity:GetAngles()
			self.Entity:SetAngles(Angle(0,oldang.y,oldang.r)) 
		else
			phys:EnableMotion(true)
		end
	end
end

function ENT:Enabled()
	return false;
end

function ENT:FireGuns()
end

function ENT:FireWeapon()
	if (StarGate.Installed == true) then
		local pos = self.Entity:GetPos();
		local vel = self.Entity:GetVelocity();
		local up = self.Entity:GetUp();
		local forward = self.Entity:GetForward();

		--local side = self.Entity:GetUp()*270;  -- Not needed ?
		local e = ents.Create("staff_pulse_stationary");
		e.Parent = self.Entity;
		e:SetPos( self.Entity:GetPos() + self.Entity:GetUp()*70 + self.Entity:GetForward()*50 + self.Entity:GetRight()*-100 ); --borrowed from LightDemon [WAYYYY better than the old stargate positioning system]
		e:SetAngles(self.Entity:GetAngles());
		e:SetOwner(self.Entity); 
		e.Owner = self.Entity.Owner;
		e:Spawn();

		e:SetVelocity(vel + (forward*2000));
		e.CannonVelocity = vel;
		
		local e2 = ents.Create("staff_pulse_stationary");
		e2.Parent = self.Entity;
		e2:SetPos( self.Entity:GetPos() + self.Entity:GetUp()*70 + self.Entity:GetForward()*50 + self.Entity:GetRight()*100 ); --borrowed from LightDemon [WAYYYY better than the stargate positioning system]
		e2:SetAngles(self.Entity:GetAngles());
		e2:SetOwner(self.Entity); 
		e2.Owner = self.Entity.Owner;
		e2:Spawn();

		e2:SetVelocity(vel + (forward*2000));
		e2.CannonVelocity = vel;
		
		
		self.Entity:EmitSound(Sound("weapons/staff_weapon.mp3"),90,math.random(90,110));
	else
		self.UseWeapon = false
	end

end


function ENT:TriggerInput(k,v)
	if(k=="Disable Weapon") then
		if ((v or 0) >= 1) then
			self.UseWeapon = false
		else
			self.UseWeapon = true
		end
	end
	
	if(k=="View") then
		if((v or 0) >= 1) then
			self.Entity:SetNetworkedBool("FirstPerson",true)
		else
			self.Entity:SetNetworkedBool("FirstPerson",false)
		end
	end
	
end	


 function ENT:ShowOutput()

end 
